#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from __future__ import absolute_import, unicode_literals

import os
from celery import Celery
from celery.schedules import crontab
from iClouds import settings, celeryconfig

# import os, django
# from celery import Celery
# from django.utils import timezone
# from django.conf import settings
# from django.apps import apps
# from celery.schedules import crontab
# from datetime import timedelta

# pip install django_celery_beat
# 设置django环境
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "iClouds.settings")

# django重载
# django.setup()

# 创建celery app
app = Celery("iClouds")

#  使用CELERY_ 作为前缀，在settings中写配置
# app.config_from_object("django.conf.settings", namespace="CELERY")
# app.config_from_object(settings, namespace="CELERY")
# app.config_from_object(settings)
app.config_from_object(celeryconfig)

# 发现任务文件每个app下的task.py
app.autodiscover_tasks()
# app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
# app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])

# 是否使用UTC
# app.conf.enable_utc = False

# 解决时区问题,定时任务启动就循环输出
# app.now = timezone.now

app.conf.update(
    CELERYBEAT_SCHEDULE={
        # "myTest": {
        #     "task": "mysqldb.tasks.MyCeleryTest",
        #     # "schedule": settings.timedelta(seconds=60),
        #     "schedule": crontab(minute=23),
        #     "args": (),
        #     # "kwargs": None,
        # },
        # "初始化启动任务": {
        #     "task": "mysqldb.tasks.MyInitialize",
        #     "schedule": crontab(minute=10, hour=0),
        #     "args": (),
        # },
        "CheckWorkItemList": {  # 自动点检未提交的事务
            "task": "PlaceManager.tasks.CheckWorkItemList",
            "schedule": crontab(hour=16,minute=10), # 系统默认UTC时区
            "args": (),
        },
        "myWorkItemModes": {  # 自动创建事务从模板
            "task": "PlaceManager.tasks.CheckWorkItemModel",
            "schedule": crontab(hour=15,minute=0), # 系统默认UTC时区
            "args": (),
        },
        "CheckWorkItemListAlert": {  # 自动发送工作项事务提醒通知
            "task": "PlaceManager.tasks.CheckWorkItemListAlert",
            "schedule": settings.timedelta(seconds=60),
            "args": (),
        },
        # "ConvertInterAuto": {  # 自动兑换积分
        #     "task": "StaffManager.tasks.ConvertInterAuto",
        #     "schedule": crontab(minute=30, hour=0),
        #     "args": (),
        # },
    }
)

# app.conf.beat_schedule = {
#     "test": {
#         "task": "mysqldb.tasks.MyCeleryTest",
#         "schedule": timedelta(seconds=20),
#         # "schedule": crontab(minute=0, hour=23),
#         "args": (),
#         # "kwargs": None,
#     },
# }

# app.conf.update(
#     CELERYBEAT_SCHEDULE={
#         "Check_timeout_reqsn": {
#             "task": "BarManger.tasks.Check_timeout_reqsn",
#             "schedule": timedelta(seconds=60),
#             "args": (),
#         },
#         "Check_update_sync": {
#             "task": "BarManger.tasks.Check_update_sync",
#             "schedule": timedelta(seconds=60),
#             "args": (),
#         },
#     }
# )


# 调用异步任务,返回任务ID
# res=MyCeleryTest.delay(args=[3,4])
# res=MyCeleryTest.apply_async(args=[3,4])
# res=MyCeleryTest.apply_async(kwds={'a':4,'b':4})
# print(res)  # 任务id号 :1f9faa34-452c-48df-b874-1c2824839675  查询结果的
